package com.example.wto.myapplication;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.List;
import java.util.Random;

public class TeacherAdapter extends ArrayAdapter<DayOfWeek>
{
    private Context context=null;
    private int resource;
    private List<DayOfWeek> weeks;
    private String T_COLOR[]={"#FE4365","#FC9D9A","#F8CDAD","#C8C8A9","#83AF9B"};
    public TeacherAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<DayOfWeek> objects)
    {
        super(context, resource, objects);
        this.context=context;
        this.resource=resource;
        this.weeks=objects;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
    {
        DayOfWeek weekDay=weeks.get(position);
        if(convertView==null)
        {
            convertView = LayoutInflater.from(context).inflate(resource, parent,false);
        }
        TextView weekName_tv=(TextView)convertView.findViewById(R.id.week_name_textview);
        TextView one_class_tv=(TextView)convertView.findViewById(R.id.one_class_textview);
        TextView two_class_tv=(TextView)convertView.findViewById(R.id.two_class_textview);
        TextView three_class_tv=(TextView)convertView.findViewById(R.id.three_class_textview);
        TextView four_class_tv=(TextView)convertView.findViewById(R.id.four_class_textview);

        weekName_tv.setText(weekDay.getWeekName());
        String class_one=weekDay.getCourseList().get(0).getCourseName();
        String class_two=weekDay.getCourseList().get(1).getCourseName();
        String class_three=weekDay.getCourseList().get(2).getCourseName();
        String class_four=weekDay.getCourseList().get(3).getCourseName();
        //设置每节课的内容
        one_class_tv.setText(class_one);
        two_class_tv.setText(class_two);
        three_class_tv.setText(class_three);
        four_class_tv.setText(class_four);
        //清空背景颜色
        one_class_tv.setBackgroundColor(Color.WHITE);
        two_class_tv.setBackgroundColor(Color.WHITE);
        three_class_tv.setBackgroundColor(Color.WHITE);
        four_class_tv.setBackgroundColor(Color.WHITE);
        //设置每节课的背景颜色
        if(!class_one.equals(""))
        {
            Random random=new Random(7);
            int color_index=random.nextInt(5);
            one_class_tv.setBackgroundColor(Color.parseColor(T_COLOR[color_index]));
        }
        if(!class_two.equals(""))
        {
            Random random=new Random(3);
            int color_index=random.nextInt(5);
            two_class_tv.setBackgroundColor(Color.parseColor(T_COLOR[color_index]));
        }
        if(!class_three.equals(""))
        {
            Random random=new Random(5);
            int color_index=random.nextInt(5);
            three_class_tv.setBackgroundColor(Color.parseColor(T_COLOR[color_index]));
        }
        if(!class_four.equals(""))
        {
            Random random=new Random(8);
            int color_index=random.nextInt(5);
            four_class_tv.setBackgroundColor(Color.parseColor(T_COLOR[color_index]));
        }
        return convertView;
    }
}
